<template>
  <div class="department-container">
    <div class="app-container">
      <!-- 头部 -->
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
      </el-card>
      <!-- 主体 -->
      <el-tree
        :data="list"
        :default-expand-all="true"
      >
        <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
        <!--
      什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (类似于table 单元格数据渲染)
    -->
        <template #default="{ data }">
          <el-row
            type="flex"
            justify="space-between"
            align="middle"
            style="height: 40px; width: 100%;"
          >
            <el-col :span="20">
              <span>{{ data.name }}</span>
            </el-col>
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <!-- 两个内容 -->
                <el-col>{{ data.manager }}</el-col>
                <el-col>
                  <!-- 下拉菜单 element -->
                  <el-dropdown>
                    <span> 操作<i class="el-icon-arrow-down" /> </span>
                    <!-- 下拉菜单 -->
                    <el-dropdown-menu slot="dropdown">
                      <!-- 点击时获取当前部门id -->
                      <el-dropdown-item @click.native="addFn(data.id)">添加子部门</el-dropdown-item>
                      <el-dropdown-item @click.native="EditFn(data.id)">编辑部门</el-dropdown-item>
                      <el-dropdown-item v-if="data.children.length===0" @click.native="delFn(data.id)">删除部门</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <el-dialog
      title="添加或编辑"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="showDialog"
    >
      <!-- 将id数据传给子组件 -->
      <addor-eidt
        v-if="showDialog"
        :id="curId"
        :origin-list="originList"
        :is-edit="isEdit"
        @update-depart="hUpdateDepart"
        @close="hClose"
      />
    </el-dialog>
  </div>
</template>

<script>
import { getdepartment, delDepartment } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import AddorEdit from './deptDialog.vue'
export default {
  components: {
    'addor-eidt': AddorEdit
  },
  data() {
    return {
      curId: '',
      list: [], // 组织架构数据
      showDialog: false,
      isEdit: false// 是否处于编辑状态
    }
  },
  created() {
    this.setdepartment() // 获取组织架构数据
  },
  methods: {
    async setdepartment() {
      try {
        const res = await getdepartment()
        console.log(res)
        // 由于业务中传智教育的pid是:-1，它是最高级的节点
        // shift 从数组头部取出一个元素
        const root = res.data.depts.shift()
        console.log('这是root', root)
        // 筛选验证需要数据
        this.originList = res.data.depts.map(({ id, pid, name, code }) => ({ id, pid, name, code }))
        // 用工具函数把 平铺的数组转成 树型数据
        root.children = tranListToTreeData(res.data.depts)
        this.list = [root]
      } catch (err) {
        console.log(err)
      }
    },
    // 添加
    addFn(id) {
      this.curId = id
      this.showDialog = true
      this.isEdit = false
    },
    // 编辑
    EditFn(id) {
      this.showDialog = true
      // 2. 表单中要显示详情数据
      // 把id传给子组件，在子组件中去获取详情
      this.curId = id
      // 3. 传递一个标识给子组件，告诉它我们现在是编辑状态
      this.isEdit = true
    },
    // 删除
    async getDel(id) {
      try {
        const res = await delDepartment(id)
        console.log(res)
        // 1. 提示
        this.$message({ type: 'success', message: '删除成功!' })
        // 2. 更新数据
        this.setdepartment()
      } catch (err) {
        console.log(err)
        this.$message({ type: 'error', message: '删除失败' })
      }
    },
    delFn(id) {
      this.$confirm('此操作将永久删除部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 做删除
        this.getDel(id)
      }).catch(() => {

      })
    },
    hUpdateDepart() {
      // 再次关闭弹层
      this.showDialog = false
      // 重新加载数据
      this.setdepartment()
    },
    hClose() {
      this.showDialog = false
    }
  }
}
</script>

<style>

</style>
